package com.chendx.store.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chendx.model.common.dtos.ResponseResult;
import com.chendx.model.common.enums.AppHttpCodeEnum;
import com.chendx.model.user.pojo.Address;
import com.chendx.model.user.pojo.User;
import com.chendx.store.user.mapper.AddressMapper;
import com.chendx.store.user.service.AddressService;
import com.chendx.store.utils.AppThreadLocalUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Created by chendx on 2022/4/19.
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper,Address> implements AddressService {


    @Override
    public ResponseResult add(Address address) {
        User user = AppThreadLocalUtils.get();
        if (user == null || user.getId() == null || user.getUsername() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        String username = user.getUsername();
        String userId = String.valueOf(user.getId());
        if (address == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        address.setUsername(username);
        save(address);
        return ResponseResult.okResult(address);
    }

    @Override
    public ResponseResult userAddress() {
        User user = AppThreadLocalUtils.get();
        if (user == null || user.getId() == null || user.getUsername() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        String username = user.getUsername();
        String userId = String.valueOf(user.getId());
        List<Address> addresses = list(Wrappers.<Address>lambdaQuery().eq(Address::getUsername, username));
        return ResponseResult.okResult(addresses);
    }

    @Override
    public ResponseResult defaultAddress() {
        User user = AppThreadLocalUtils.get();
        if (user == null || user.getId() == null || user.getUsername() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        String username = user.getUsername();
        String userId = String.valueOf(user.getId());
        Address address = getOne(Wrappers.<Address>lambdaQuery()
                .eq(Address::getUsername, username)
                .eq(Address::getIsDefault, "1"));
        return ResponseResult.okResult(address);
    }

    @Override
    public ResponseResult setDefaultAddress(Integer id) {
        User user = AppThreadLocalUtils.get();
        if (user == null || user.getId() == null || user.getUsername() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        String username = user.getUsername();
        String userId = String.valueOf(user.getId());
        if (StringUtils.isEmpty(id)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        update(Wrappers.<Address>lambdaUpdate().set(Address::getIsDefault,"0").eq(Address::getUsername, username));
        update(Wrappers.<Address>lambdaUpdate()
                .set(Address::getIsDefault,"1")
                .eq(Address::getUsername, username).eq(Address::getId,id));
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
